// (c) 2005 Wout de Zeeuw

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;

namespace CaveArt.UIcomp
{
	/// <summary>
	/// Just a panel that contains progress controls.
	/// Wrote this because the normal StatusBar is annoyingly restrictive.
	/// </summary>
	public class StatusBarEx : Panel
	{
		/// <summary>
		/// Constructor.
		/// </summary>
		public StatusBarEx() {
			Height = 20;
			Dock = DockStyle.Bottom;
		}
		
		/// <summary>
		/// Add an item.
		/// </summary>
		/// <param name="absoluteWidth">
		/// The absolute portion of the total width.
		/// </param>
		/// <param name="relativeWidth">
		/// The relative portion of the total width (between 0 and 1).
		/// </param>
		public void AddItem(Control control, int absoluteWidth, float relativeWidth) {
			StatusBarItem item = new StatusBarItem(control, absoluteWidth, relativeWidth);
			Controls.Add(item);
		}
		
		/// <summary>
		/// Layout items from left to right.
		/// </summary>
		protected override void OnLayout(LayoutEventArgs e) {
			// Calculate left over width.
			float leftOverWidth = ClientSize.Width;
			foreach (StatusBarItem item in Controls) {
				leftOverWidth -= item.AbsoluteWidth;
			}
			if (leftOverWidth < 0f) {
				leftOverWidth = 0f;
			}
			// Now really layout.
			int pos = 0;
			foreach (StatusBarItem item in Controls) {
				int itemwidth = item.AbsoluteWidth + (int)(leftOverWidth * item.RelativeWidth);
				item.Bounds = new Rectangle(pos, 0, itemwidth, Height);
				pos += itemwidth;
			}
			base.OnLayout(e);
		}
	}
}
